#include <iostream>
#include <string>
using namespace std;

const int N = 10, M = 1e5 + 10;

int dp[N];
string a[M];

int main()
{
  int n;
  cin >> n;
  for(int i = 1; i <= n; i++) cin >> a[i];
  for(int i = 1; i <= n; i++)
  {
    string s = a[i];
    int x = s[0] - '0',y = s[s.size() - 1] - '0';
    dp[y] = max(dp[x] + 1, dp[y]);
  }
  int m = 0;
  for(int i = 0; i <= 9; i++)
  {
    m = max(m, dp[i]);
  }
  cout << n - m << endl;
  return 0;
}
